﻿using System.Data;
using SqlSugar;
using StoreAPI.Models;

namespace StoreAPI.Services.ServerImpls
{
    public class StoreAreaService : IStoreAreaService
    {
        private readonly ISqlSugarClient _db;

        public StoreAreaService(ISqlSugarClient db)
        {
            _db = db;
        }

        //获取最大ID
        public int GetMaxId()
        {
            return _db.Queryable<StoreArea>()
               .Max(x => x.StoreAreaId);
        }
        //判断是否存在
        public bool Exists(int model)
        {
            return _db.Queryable<StoreArea>()
               .Any(x => x.StoreAreaId == model);
        }
        //注册
        public bool Add(StoreArea model)
        {
            return _db.Insertable<StoreArea>(model).ExecuteCommand() > 0;
        }
        //更新
        public bool Update(StoreArea model)
        {
            return _db.Updateable<StoreArea>(model).ExecuteCommand() > 0;
        }
        //删除
        public bool Delete(int primaryKey)
        {
            return _db.Deleteable<StoreArea>(primaryKey).ExecuteCommand() > 0;
        }
        //批量删除
        public bool DeleteList(int[] primaryKeys)
        {
            return _db.Deleteable<StoreArea>().In(primaryKeys).ExecuteCommand() > 0;
        }
        //查询单个
        public StoreArea GetModel(int primaryKey)
        {
            return _db.Queryable<StoreArea>()
               .InSingle(primaryKey);
        }
        //查询一行数据转StoreArea模型
        public StoreArea DataRowToModel(DataRow row)
        {


            StoreArea model = new StoreArea();
            if (row != null)
            {
                if (row["StoreAreaId"] != null && row["StoreAreaId"].ToString() != "")
                {
                    model.StoreAreaId = int.Parse(row["StoreAreaId"].ToString());
                }
                if (row["StoreAreaNo"] != null)
                {
                    model.StoreAreaNo = row["StoreAreaNo"].ToString();
                }
                if (row["StoreAreaName"] != null)
                {
                    model.StoreAreaName = row["StoreAreaName"].ToString();
                }
                if (row["StoreId"] != null && row["StoreId"].ToString() != "")
                {
                    model.StoreId = int.Parse(row["StoreId"].ToString());
                }
                if (row["Temperature"] != null && row["Temperature"].ToString() != "")
                {
                    model.Temperature = decimal.Parse(row["Temperature"].ToString());
                }
                if (row["MinTemperature"] != null && row["MinTemperature"].ToString() != "")
                {
                    model.MinTemperature = decimal.Parse(row["MinTemperature"].ToString());
                }
                if (row["MaxTemperature"] != null && row["MaxTemperature"].ToString() != "")
                {
                    model.MaxTemperature = decimal.Parse(row["MaxTemperature"].ToString());
                }
                if (row["TemperatureStatus"] != null && row["TemperatureStatus"].ToString() != "")
                {
                    model.TemperatureStatus = int.Parse(row["TemperatureStatus"].ToString());
                }
                if (row["Remark"] != null)
                {
                    model.Remark = row["Remark"].ToString();
                }
                if (row["Status"] != null && row["Status"].ToString() != "")
                {
                    model.Status = int.Parse(row["Status"].ToString());
                }
                if (row["CreateUserId"] != null && row["CreateUserId"].ToString() != "")
                {
                    model.CreateUserId = int.Parse(row["CreateUserId"].ToString());
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
                if (row["LastUpdateUserId"] != null && row["LastUpdateUserId"].ToString() != "")
                {
                    model.LastUpdateUserId = int.Parse(row["LastUpdateUserId"].ToString());
                }
                if (row["LastUpdateTime"] != null && row["LastUpdateTime"].ToString() != "")
                {
                    model.LastUpdateTime = DateTime.Parse(row["LastUpdateTime"].ToString());
                }
            }
            return model;

        }
        //查询所有
        public List<StoreArea> GetListAll(Expressionable<StoreArea> exp)
        {
            return _db.Queryable<StoreArea>()
               .Where(exp.ToExpression())
               .ToList();
        }
        //获得前几行数据
        public List<StoreArea> GetList(Expressionable<StoreArea> exp, int Top)
        {
            return _db.Queryable<StoreArea>()
               .Where(exp.ToExpression())
               .Take(Top)
               .ToList();
        }
        //获取记录数
        public int GetRecordCount(Expressionable<StoreArea> exp)
        {
            return _db.Queryable<StoreArea>()
               .Where(exp.ToExpression())
               .Count();
        }
        //分页查询
        public List<StoreArea> GetListByPage(Expressionable<StoreArea> exp, out int totalPage, int pageIndex = 1, int pageSize = 10)
        {
            totalPage = 0;
            var result = _db.Queryable<StoreArea>()
               .Where(exp.ToExpression())
               .ToPageList(pageIndex, pageSize, ref totalPage);
            return result;
        }
    }
}
